//
//  SelectTableView.m
//  预登记系统
//
//  Created by mac on 16/4/11.
//  Copyright © 2016年 Simon. All rights reserved.
//

#import "SelectTableView.h"
#import "SelectTableViewCell.h"
#import "NSArray+Chinese.h"

@implementation SelectTableView
{
    NSArray *keyArr;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        
        self.allowsSelection = YES;
//        self.userInteractionEnabled = YES;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
    }
    return self;
}

- (void)setCellData:(id)cellData {
    
    _cellData = cellData;
    
    if ([_cellData respondsToSelector:@selector(allKeys)]) {
        keyArr = [[_cellData allKeys] sortedWithChineseKey:nil];
    } else {
        keyArr = [NSArray arrayWithArray:_cellData];
    }
}

#pragma -mark -UITableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (keyArr) {
        return keyArr.count;
        //    NSLog(@"%lu", (unsigned long)_cellData.count);
    } else {
        
        return 0;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

//    NSLog(@"height");
    return kCellHeight;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    
//    return 0;
//}
//
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    
//    return 0;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"cell");
    UITableViewCell *tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableViewCell"];

    tableViewCell.textLabel.text = keyArr[indexPath.row];
    tableViewCell.textLabel.font = [UIFont systemFontOfSize:12];
    tableViewCell.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
    return tableViewCell;
    
//    SelectTableViewCell *cell = [[SelectTableViewCell alloc] init];
//    
//    cell.textLabel.text = _cellData[indexPath.row];
//    
//    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SelectFinishedBlock selectFinished = _selectFinishBolck;
    if (selectFinished&&(_tableViewType == 3)) {
        
        selectFinished(keyArr[indexPath.row]);
    }
    SelectFinishedBlock selectForNext = _selectForNextBlock;
    if (selectForNext&&(_tableViewType < 3)) {
        selectForNext(keyArr[indexPath.row]);
    }
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
